package com.g0dkar.leet.core;

import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import br.com.caelum.vraptor.ioc.ApplicationScoped;
import br.com.caelum.vraptor.ioc.Component;

import com.g0dkar.leet.core.configuration.LeetConfiguration;
import com.g0dkar.leet.core.model.ClassMapping;
import com.g0dkar.leet.persistence.PersistenceManager;

/**
 * This class is the heart of the Leet Framework.
 * 
 * @author g0dkar
 *
 */
@Component
@ApplicationScoped
public final class Leet {
	protected static Leet INSTANCE;
	
	public static Leet getInstance() {
		return INSTANCE;
	}
	
	private LeetConfiguration configuration;
	
	public Leet() {
		this(false);
	}
	
	public Leet(boolean asynchronous) {
		init(asynchronous);
	}
	
	public void init(boolean asynchronous) {
		LeetStartup startup = new LeetStartup(this);
		
		if (asynchronous) {
			new Thread(startup).start();
		}
		else {
			startup.run();
		}
	}
	
	public PersistenceManager getPersistenceManager() {
		return configuration.getPersistenceManager();
	}

	public LeetConfiguration getConfiguration() {
		return configuration;
	}

	public void setConfiguration(LeetConfiguration configuration) {
		this.configuration = configuration;
	}

	public String getName() {
		return configuration.getName();
	}

	public String getDescription() {
		return configuration.getDescription();
	}

	public Map<String, ClassMapping> getClassMappings() {
		return configuration.getClassMappings();
	}

	public ClassMapping getClassMapping(String mappingName) {
		return configuration.getClassMapping(mappingName);
	}

	public ClassMapping getClassMapping(Class<?> klass) {
		return configuration.getClassMapping(klass);
	}
	
	public boolean hasClassMapping(String mappingName) {
		return configuration.getClassMappings().containsKey(mappingName);
	}

	public void addClassMapping(ClassMapping classMapping) {
		configuration.addClassMapping(classMapping);
	}

	public void addClassMapping(Class<?> klass) {
		configuration.addClassMapping(klass);
	}

	public void addClassMappingPackage(String packageName) {
		configuration.addClassMappingPackage(packageName);
	}

	public Properties getProperties() {
		return configuration.getProperties();
	}

	public String getProperty(String key) {
		return configuration.getProperty(key);
	}

	public Locale getLocale() {
		return configuration.getLocale();
	}
}
